package com.cljz.peipei.ui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.cljz.peipei.R;
import com.cljz.peipei.base.App;
import com.cljz.peipei.base.BaseActivity;
import com.cljz.peipei.base.BaseP;
import com.cljz.peipei.utils.ConstellationUtils;
import com.cljz.peipei.utils.date.GetSystemTimeUtil;
import com.cljz.peipei.widget.PersonTimeSelector;
import com.clzj.peipei.bean.UserInfo;
import com.jakewharton.rxbinding.view.RxView;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.router.Router;
import rx.functions.Action1;

/**
 * Created by apple on 2017/4/18.
 */

public class SetAgeActivity extends BaseActivity{

        @BindView(R.id.fl_back)
        FrameLayout flBack;
        @BindView(R.id.tv_constellation)
        TextView tvConstellation;
        @BindView(R.id.tv_age)
        TextView tvAge;
        @BindView(R.id.btn_next)
        Button btnNext;
        private Bundle bundle;
        private String currentDate = "1997-01-01";

        @Override
        public void initData(Bundle savedInstanceState) {
            bundle = getIntent().getExtras();
            BaseP<SetAgeActivity> p = new BaseP<>();
            UserInfo userInfo = p.getUserInfo(p.getToken());
            String birthday = userInfo.getBirthday();
            tvAge.setText(birthday);
            tvConstellation.setText(ConstellationUtils.getConstellation(birthday.replace(".","-")));
        }

        @Override
        protected void initListener() {
            super.initListener();


            RxView.clicks(tvAge)
                    .compose(this.<Void>bindToLifecycle())
                    .subscribe(new Action1<Void>() {
                        @Override
                        public void call(Void aVoid) {
                            new PersonTimeSelector(SetAgeActivity.this, new PersonTimeSelector.ResultHandler() {
                                @Override
                                public void handle(String time) {
                                    tvAge.setText(time.replace("-","."));
                                    tvConstellation.setText(ConstellationUtils.getConstellation(time));
                                    currentDate = time;
                                }
                            }, GetSystemTimeUtil.getStart100Time(), GetSystemTimeUtil.getStartTime(),currentDate).show();
                        }
                    });

            RxView.clicks(btnNext)
                    .compose(this.<Void>bindToLifecycle())
                    .subscribe(new Action1<Void>() {
                        @Override
                        public void call(Void aVoid) {
                            bundle.putString("age", tvAge.getText().toString().trim());
                            bundle.putString("constellation", ConstellationUtils.getStr(tvConstellation.getText().toString()));
                            Router.newIntent((Activity) App.getInstance())
                                    .to(SetNicknameActivity.class)
                                    .data(bundle)
                                    .launch();
                        }
                    });
            RxView.clicks(flBack)
                    .compose(this.<Void>bindToLifecycle())
                    .subscribe(new Action1<Void>() {
                        @Override
                        public void call(Void aVoid) {
                            Router.pop(SetAgeActivity.this);
                        }
                    });
        }

        @Override
        public int getLayoutId() {
            return R.layout.activity_set_age;
        }

        @Override
        public Object newP() {
            return null;
        }


}
